package com.foxit.adapp;
import java.util.ArrayList;
import java.util.Random;
import java.util.Arrays;

public class GroupAD {
    private String udid;
    private ArrayList<String> adlist;
    private ArrayList<GroupUnit> gu;

    public GroupAD() {
    }
    public GroupAD(String udid) {
         this.udid = udid;
    }
    public GroupAD(String udid,String groupset) {
         this.udid = udid;
         if(groupset==null || groupset.length()==0) return;
         this.gu = new  ArrayList<GroupUnit>();
         String[] gs= groupset.split(":");
         for(int i=0;i < gs.length;i++) {
             GroupUnit u = new GroupUnit(gs[i]);
             this.gu.add(u);
         }
    }
    public void setGroupInfo(String gi) {
         if(gi==null || gi.length()==0) return;
         this.adlist = new ArrayList<String>(Arrays.asList(gi.split(";")));
    }

    public ArrayList<String> getAD() {
         if (adlist == null) return null;
         return adlist;
    }
    public String[] getAD1(String userinfo) {
         if ( gu == null) return null;
         if(userinfo==null || userinfo.length()==0) return null;
         String[] u = userinfo.split(";");
         ArrayList<String[]> glist= new ArrayList<String[]>(); 
         for(int i=0; i < u.length;i++) {
            String[] a= u[i].split(",");
            glist.add(a);
         } 
         ArrayList<String[]> adl=new ArrayList<String[]>();
         int x;
          for(int j=0;j < glist.size();j++) {
             x = Integer.valueOf(glist.get(j)[0]);
             if(x <= 0) continue;
             if(x > 3) x=3;
             for(int i=0;i < gu.size();i++) {
                if(gu.get(i).getName().indexOf(glist.get(j)[1]) >=0) {
                   String [] a= new String[2];
                   a[0]=gu.get(i).getAD(x);
                   a[1]=gu.get(i).getDimension();
                   adl.add(a);
                }
             }
          }
          if(adl.size() <= 0) return null;
          Random rand = new Random();
          return adl.get(rand.nextInt(adl.size()));
    }
    public ArrayList<String[]> getADS(String userinfo,int maxno) {
         if ( gu == null) return null;
         if(userinfo==null || userinfo.length()==0) return null;
         String[] u = userinfo.split(";");
         ArrayList<String[]> glist= new ArrayList<String[]>();
         for(int i=0; i < u.length;i++) {
            String[] a= u[i].split(",");
            glist.add(a);
         }
         ArrayList<String[]> adl=new ArrayList<String[]>();
         int x=0;
         if(maxno==0) maxno=999;
         int m=0;
         for(int j=0;j < glist.size();j++) {
            x = Integer.valueOf(glist.get(j)[0]);
            if(x <= 0) continue;
            if(x > 3) x=3;
            for(int i=0;i < gu.size();i++) {
               if(gu.get(i).getName().indexOf(glist.get(j)[1]) >=0) {
                  String [] a= new String[2];
                  a[0]=gu.get(i).getAD(x);
                  a[1]=gu.get(i).getDimension();
                  adl.add(a);
                  if(++m >= maxno) break;
               }
            }
            if(m >= maxno) break;
         }
         return adl;
    }
    
}
